//
//  APIServiceTest.swift
//  LoopTests
//
//  Created by thomas on 2020/9/23.
//

import XCTest
@testable import Loop

class APIServiceTest: XCTestCase {

    override func setUpWithError() throws {
    }

    override func tearDownWithError() throws {
    }

    func testSharedInstanceIsInitialized() throws {
        XCTAssertNotNil(APIService.shared)
    }

    func testHandleRequestResultWithHttpError() throws {

        let httpError = NSError(domain: "com.cgg.remote",
                            code: 500,
                            userInfo: [NSLocalizedDescriptionKey: "Server Internal Error"])

        let expectation = self.expectation(description: "testHandleRequestResultWithHttpError")
        APIService.shared.handleRequestResult(data: nil, response: nil, error: httpError) { dict, error in

            XCTAssertNil(dict)
            XCTAssertNotNil(error)
            XCTAssertEqual(httpError, error)

            expectation.fulfill()
        }

        self.waitForExpectations(timeout: 1, handler: nil)
    }

    func testHandleRequestResultWithEmptyData() throws {

        let expectation = self.expectation(description: "testHandleRequestResultWithEmptyData")
        APIService.shared.handleRequestResult(data: nil, response: nil, error: nil) { dict, error in

            XCTAssertNil(dict)
            XCTAssertNil(error)

            expectation.fulfill()
        }

        self.waitForExpectations(timeout: 1, handler: nil)
    }

    func testHandleRequestResultWithJSONParseError() throws {

        let json = "any string".data(using: .utf8)
        let expectation = self.expectation(description: "testHandleRequestResultWithJSONParseError")
        APIService.shared.handleRequestResult(data: json, response: nil, error: nil) { dict, error in

            XCTAssertNil(dict)
            XCTAssertNotNil(error)
            XCTAssertEqual(error?.code, -2)
            XCTAssertEqual(error?.localizedDescription, "解析json数据出错")

            expectation.fulfill()
        }

        self.waitForExpectations(timeout: 1, handler: nil)
    }

    func testHandleRequestResultSucceed() throws {

        let json = """
            {
              "current_user_url": "https://api.github.com/user",
              "current_user_authorizations_html_url": "https://github.com/settings/connections/applications{/client_id}",
              "authorizations_url": "https://api.github.com/authorizations"
            }
        """.data(using: .utf8)

        let expectation = self.expectation(description: "testHandleRequestResultSucceed")
        APIService.shared.handleRequestResult(data: json, response: nil, error: nil) { dict, error in

            XCTAssertNil(error)
            XCTAssertNotNil(dict)
            XCTAssertEqual(dict?.count, 3)
            XCTAssertEqual(dict?["current_user_url"], "https://api.github.com/user")
            XCTAssertEqual(dict?["current_user_authorizations_html_url"], "https://github.com/settings/connections/applications{/client_id}")
            XCTAssertEqual(dict?["authorizations_url"], "https://api.github.com/authorizations")

            expectation.fulfill()
        }

        self.waitForExpectations(timeout: 1, handler: nil)
    }

    func testFeatchData() throws {

        let expectation = self.expectation(description: "testFeatchData")
        APIService.shared.fetchData(method: .get) { (dict, error) in

            XCTAssertNil(error)
            XCTAssertNotNil(dict)

            expectation.fulfill()

        }

        self.waitForExpectations(timeout: 20, handler: nil)
    }
}

